package ua.dogar.test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.util.Arrays;

import ua.dogar.exceptions.IncompatibleVectorSizesException;
import ua.dogar.patterns.ArrayVectorFactory;
import ua.dogar.patterns.JVectorAdapter;
import ua.dogar.patterns.VectorFactory;
import ua.dogar.vectors.ArrayVector;
import ua.dogar.vectors.LinkedListVector;
import ua.dogar.vectors.MySynchronizedVector;
import ua.dogar.vectors.ProtectedVector;
import ua.dogar.vectors.Vector;



public class Vectors {
		static	private VectorFactory vectorFac = new ArrayVectorFactory();

	  public static Vector mulVector(Vector vec,int num){
		  		Vector vector = createInstance(vec.getLength());
		  		try {
					vector.setElementsFromVector(vec);
				} catch (IncompatibleVectorSizesException e) {
					e.printStackTrace();
					System.out.println("Wrong size!");
				}
		  		vector.mulVector(num);
	        	return vector;
	    }
	  public static void setVectorFactory(VectorFactory vector){
		  vectorFac=vector;
	  }
	  public static Vector addVectors(Vector vec1,Vector vec2){
		  Vector result = createInstance(vec1.getLength());
		  if(vec1.getLength() == vec2.getLength()){
	            for (int i = 0;i<vec1.getLength();i++)
	            		result.setElement( vec2.getElement(i)+vec1.getElement(i),i);
	            		return result;
	        }
	        else return null;
	    }
	  public static void sortVector(Vector  vec){
          for (int i = vec.getLength() - 1; i >= 2; i--) {
                   boolean sorted = true;
              for (int j = 0; j < i; j++) {
                          if (vec.getElement(j) > vec.getElement(j+1)) {
                      double temp = vec.getElement(j);
                      vec.setElement(vec.getElement(j+1),j ); 
                      vec.setElement(temp,j+1); 
                      sorted = false;
                  }
              }
              if(sorted) {
                  break;
              }
          }
      }
	  static Vector generateVector(int length){
		  double[] tempArray = new double[length];
		  for(int i = 0;i<length;i++)
			 tempArray[i]= (Math.random()*10);
		  Vector myVec = createInstance();
		  try {
			myVec.setElementsFromArray(tempArray);
		} catch (IncompatibleVectorSizesException e) {
			e.printStackTrace();
			System.out.println("Wrong size!");
		}
		  return myVec;
	  }
	 public static void outputVector(Vector v,OutputStream out) throws IOException{
		 DataOutputStream outputStream = new DataOutputStream(out);
		 outputStream.writeInt(v.getLength());
		 for(Double value:v)outputStream.writeDouble(value);
	 }
	 public static Vector inputVector(InputStream in) {
			try {
		 DataInputStream inputStream = new DataInputStream(in);
		 int length;
	
			length = inputStream.readInt();
		
		 if(length == 0) return null;
		 Vector vec = createInstance(length);
		 for(int i = 0;i<length;i++)
			 vec.setElement(inputStream.readDouble(), i);
		 return vec;
			} catch (IOException e) {
			return null;
			}
	 }
	 public static void writeVector(Vector v, Writer out) throws IOException{
		  	StringBuffer stringOut = new StringBuffer();
		  	stringOut.append(v.getLength());
		  	for(int i =0;i<v.getLength();i++)
			stringOut.append(" "+v.getElement(i));
		  	stringOut.append("\n");
		  	out.write(stringOut.toString());
		  	out.flush();
	  }
	  public static Vector readVector (Reader in) throws IOException{
		    StreamTokenizer st = new StreamTokenizer(in);
		    st.nextToken();
		    int length = (int) st.nval;
		    if(length == 0) return null;
	  
	    	Vector vec = createInstance(length);
	        for (int i = 0; i < length; i++) {
	            st.nextToken();
	            vec.setElement(st.nval, i);
	        }
	        return vec;
	       
		}
	  public   static   Vector createInstance(int size){
		  return vectorFac.createVector(size);
	  }
	  public   static   Vector createInstance(){
		  return vectorFac.createVector();
	  }
	  public   static   Vector  getAdaptedJVector(java.util.Vector jVector){
		return new JVectorAdapter(jVector);
	  }
	  public   static   Vector getProtectedVector(LinkedListVector  listVec){
		return new ProtectedVector(listVec);
		  
	  }
	  public   static   Vector getSynchronizedVector(ArrayVector  vec){
			return new MySynchronizedVector(vec);
			  
		  }
}
